/*
 * @Description: 全宗上下文
 * @Author: Rfan
 * @Date: 2022-01-08 16:30:05
 * @LastEditTime: 2022-06-22 17:41:13
 */

import { createContext, useContext, useEffect, useReducer } from 'react';
import {
  ADD_LEVEL_BY_ID,
  SET_AUTO_FILING_MSG,
  SET_SELECT_AT,
  SET_SELECT_AT_ID,
  SET_SELECT_FILE,
  SET_SELECT_FOND,
  SET_SELECT_FOND_ID,
  SET_SELECT_NODE_ID,
  SET_AUTO_FILING_LOADING
} from '../constants';
import { ArrangementReducer } from '../reducers/ArrangementReducer';
import { fetchLevelByATId } from '@/api/archivesType';
import { autoFilingMethod, transferInArchiveMethod } from '@/api/archives/scattered';
import { message } from 'antd';

const initialState = {
  // 当前选中的项目
  selectFond: {},
  selectAT: {},
  selectFondId: 0,
  selectATId: 0,
  selectNodeId: 0,
  selectFile: [],

  // 当前级别ID
  currentLevel: '1',

  fondsList: [],
  ATList: [],
  levelList: [],

  // 层级tab列表
  tabList: [],
  // 当前全宗OBJ
  currentFond: {},
  // 自动组卷相关
  autoFiling: {
    loading: false,
    info: {}
  }
};

const ArrangementContext = createContext();

const ArrangementContextProvider = (props) => {
  const [state, dispatch] = useReducer(ArrangementReducer, initialState);

  const actions = {
    setSelectFile(data) {
      dispatch({ type: SET_SELECT_FILE, data });
    },
    /**
     * @description: 设置选中的全宗
     */
    setSelectFond(data) {
      dispatch({ type: SET_SELECT_FOND, data });
    },

    /**
     * @description: 设置选中的档案类型
     */
    setSelectAT(data) {
      dispatch({ type: SET_SELECT_AT, data });
    },

    /**
     * @description: 设置选中的全宗
     */
    setSelectFondId(id) {
      dispatch({ type: SET_SELECT_FOND_ID, id });
    },

    /**
     * @description: 设置选中的档案类型ID
     */
    setSelectATId(id) {
      dispatch({ type: SET_SELECT_AT_ID, id });
    },

    /**
     * @description: 设置选中的树ID
     */
    setTreeNodeId(id) {
      dispatch({ type: SET_SELECT_NODE_ID, id });
    },

    /**
     * @description: 获取档案类型下所有层级
     * @param {number} id
     */
    async fetchATLevel(id) {
      const res = await fetchLevelByATId(id);
      const { data } = res;
      dispatch({ type: ADD_LEVEL_BY_ID, data, id });
    },

    autoFilingFunc(query) {
      dispatch({ type: SET_AUTO_FILING_LOADING, status: true });
      return new Promise((resolve, reject) => {
        autoFilingMethod(query)
          .then((res) => {
            const { data } = res;
            dispatch({
              type: SET_AUTO_FILING_MSG,
              data: { ...data, message: '自动组卷成功', type: 'success' }
            });
            resolve(data);
          })
          .catch((err) => {
            const { message } = err;
            dispatch({ type: SET_AUTO_FILING_MSG, data: { message, type: 'error' } });
            reject(err);
          });
      });
    },

    transferInFunc(query) {
      return new Promise((resolve, reject) => {
        transferInArchiveMethod(query)
          .then((res) => {
            message.success('流转成功');
            const { data } = res;
            dispatch({ type: SET_AUTO_FILING_MSG, data });
            resolve(data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    }
  };

  useEffect(() => {}, []);

  return (
    <ArrangementContext.Provider value={{ ...state, ...actions }}>
      {props.children}
    </ArrangementContext.Provider>
  );
};

export const useArrangementContext = () => {
  return useContext(ArrangementContext);
};

export { ArrangementContext, ArrangementContextProvider };
